//
//  HomeTableCellOfSound.m
//  aMoment
//
//  Created by dllo on 15/10/6.
//  Copyright © 2015年 LuckyHan. All rights reserved.
//

#import "HomeTableCellOfSound.h"
#import "AllHeaderFiles.h"

@implementation HomeTableCellOfSound

- (void)dealloc
{
    [_nameLabel release];
    [_coverImageView release];
    [_playImageView release];
    [_titleLabel release];
    [_usernameLabel release];
    [_likeNumField release];
    [_home release];
    [super dealloc];
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createViews];
    }
    return self;
}

- (void)createViews
{
    _nameLabel = [[UILabel alloc] init];
    _nameLabel.font = [UIFont systemFontOfSize:FONTSIZE_SMALL];
    [self.contentView addSubview:_nameLabel];
    [_nameLabel release];
    
    _coverImageView = [[UIImageView alloc] init];
    _coverImageView.backgroundColor = [UIColor grayColor];
    _coverImageView.contentMode = UIViewContentModeScaleAspectFill;
    _coverImageView.clipsToBounds = YES;
    [self.contentView addSubview:_coverImageView];
    [_coverImageView release];
    
    _playImageView = [[UIImageView alloc] init];
    _playImageView.image = [UIImage imageNamed:@"desk2_play"];
    [self.contentView addSubview:_playImageView];
    [_playImageView release];
    
    _titleLabel = [[UILabel alloc] init];
    _titleLabel.textAlignment = NSTextAlignmentCenter;
    _titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:17];
    [self.contentView addSubview:_titleLabel];
    [_titleLabel release];
    
    _usernameLabel = [[UILabel alloc] init];
    _usernameLabel.textAlignment = NSTextAlignmentCenter;
    _usernameLabel.font = [UIFont systemFontOfSize:FONTSIZE_SMALL];
    _usernameLabel.textColor = FONTCOLOR_GRAY;
    [self.contentView addSubview:_usernameLabel];
    [_usernameLabel release];
    
    _likeNumField = [[UITextField alloc] init];
    _likeNumField.enabled = NO;
    _likeNumField.textColor = FONTCOLOR_GRAY;
    _likeNumField.font = [UIFont systemFontOfSize:FONTSIZE_SMALL];
    UIImageView *leftImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, IMAGE_16, IMAGE_16)];
    leftImageView.image = [UIImage imageNamed:@"iconfont-love"];
    _likeNumField.leftView = leftImageView;
    _likeNumField.leftViewMode = UITextFieldViewModeAlways;
    [leftImageView release];
    [self.contentView addSubview:_likeNumField];
    [_likeNumField release];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    _nameLabel.frame = CGRectMake(20 * FIT_WIDTH, 15 * FIT_HEIGHT, SCREEN_WIDTH - 40 * FIT_WIDTH, 20 * FIT_HEIGHT);
    _coverImageView.frame = CGRectMake(_nameLabel.X + 35 * FIT_WIDTH, _nameLabel.Y + _nameLabel.Height + 15 * FIT_HEIGHT, _nameLabel.Width - 70 * FIT_WIDTH, 130 * FIT_HEIGHT);
    _playImageView.frame = CGRectMake(_coverImageView.X + _coverImageView.Width / 2 - IMAGE_32 / 2, _coverImageView.Y + _coverImageView.Height - IMAGE_32 / 2, IMAGE_32, IMAGE_32);
    _titleLabel.frame = CGRectMake(_nameLabel.X, _playImageView.Y + _playImageView.Height + 10 * FIT_HEIGHT, _nameLabel.Width, _nameLabel.Height);
    _usernameLabel.frame = CGRectMake(_titleLabel.X, _titleLabel.Y + _titleLabel.Height + 10 * FIT_HEIGHT, _titleLabel.Width, _nameLabel.Height);
    _likeNumField.frame = CGRectMake(_usernameLabel.X + _usernameLabel.Width - 60 * FIT_WIDTH, _usernameLabel.Y + _usernameLabel.Height + 10 * FIT_HEIGHT, 60 * FIT_WIDTH, _nameLabel.Height);
}

#pragma mark 重写set
- (void)setHome:(Home *)home
{
    if (_home != home) {
        [_home release];
        _home = [home retain];
    }
    _nameLabel.text = [NSString stringWithFormat:@"%@ · %@", _home.name, _home.enname];
    [_coverImageView setImageWithURL:[NSURL URLWithString:_home.coverimg] placeholderImage:[UIImage imageNamed:@"placeholder-image"]];
    _titleLabel.text = _home.title;
    _usernameLabel.text = [NSString stringWithFormat:@"by: %@", [_home.userinfo objectForKey:@"uname"]];
    _likeNumField.text = [NSString stringWithFormat:@" %@", _home.like];
    
//    NSString *likeNumString = [NSString stringWithFormat:@" %@", _home.like];
//    if (![likeNumString isEqual:@" 0"]) {
//        _likeNumField.text = likeNumString;
//    }
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
